package d20chat;

import java.io.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Holds the display settings for the program.
 * Primarily handles chat font and color info.
 */
public class DisplaySettings {

    /**
     * Style of font to be used in the chat window.
     */
    public String chatFont;
    /**
     * Size of font to be used for chatting.
     */
    public String chatSize;
    /**
     * Color of font to be used for chatting.
     */
    public RGB talkColor = new RGB();
    
    /**
     * Default font color.
     */
    public RGB defaultColor = new RGB();
    /**
     * Size of display.
     */
    public String displaySize;
    /**
     * Font used for display.
     */
    public String displayFont;
    /**
     * Color to be used for background in chat window.
     */
    public RGB backColor = new RGB();
    /**
     * Toggle if other users chat styles should be overwritten or not.
     */
    public boolean overwrite;
    
    private String chatSettings = "fontSettings.txt";

    
/**
     * Class constructor for DisplaySettings.
     * Stores all sorts of display settings for chatting.
     * @param theGui Current instance of d20chatui, used to modify info such as font sizes and
     * the buffer length.
     */
    public DisplaySettings(D20Chatui theGui) {
        boolean settings = (new File("fontSettings.txt")).exists();
        
        if ( settings )
        {
            try
            {
                // Create a buffer for our file to read into
                BufferedReader bin = new BufferedReader(new FileReader(chatSettings));

                // Read each setting
                String tempString = bin.readLine();
                chatFont = tempString;
                tempString = bin.readLine();
                chatSize = tempString;            
                tempString = bin.readLine();
                talkColor.red = Integer.parseInt(tempString);            
                tempString = bin.readLine();
                talkColor.green = Integer.parseInt(tempString);
                tempString = bin.readLine();
                talkColor.blue = Integer.parseInt(tempString);
                tempString = bin.readLine();
                displayFont = tempString;
                tempString = bin.readLine();
                displaySize = tempString;
                tempString = bin.readLine();
                defaultColor.red = Integer.parseInt(tempString);
                tempString = bin.readLine();
                defaultColor.green = Integer.parseInt(tempString);
                tempString = bin.readLine();
                defaultColor.blue = Integer.parseInt(tempString);
                tempString = bin.readLine();
                backColor.red = Integer.parseInt(tempString);
                tempString = bin.readLine();
                backColor.green = Integer.parseInt(tempString);
                tempString = bin.readLine();
                backColor.blue = Integer.parseInt(tempString);
                tempString = bin.readLine();                
                overwrite = Boolean.parseBoolean(tempString);
                tempString = bin.readLine();
                theGui.buffer.setsize(Integer.parseInt(tempString));
            }
            catch (Exception e)
            {
                System.err.println ("Error reading file " + chatSettings);                
            }
        }
        else
        {
            chatFont = "Arial";
            chatSize = "12pt";
            //black
            talkColor.red = 0;
            talkColor.green = 0;
            talkColor.blue = 0;
            //black
            defaultColor.red = 0;
            defaultColor.green = 0;
            defaultColor.blue = 0;
            displaySize = "12pt";
            displayFont = "Arial";
            //white
            backColor.red = 255;
            backColor.green = 255;
            backColor.blue = 255;
            overwrite = false;
        }
    }
}


/**
 * Stores basic Red Green Blue style colors.
 */
class RGB
{
    /**
     * Stores the Red channel
     */
    public int red;
    /**
     * Stores the Green channel
     */
    public int green;
    /**
     * Stores the Blue channel
     */
    public int blue;
    /**
     * Class constructor for RGB.
     * Empty function, just starts a new instance of RGB.
     */
    public RGB()
    {
    }
}